package com.yaodian100.crm.cs.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.logger.service.LogService;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.entity.Option;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.order.entity.Orders;
import com.yaodian100.ec.order.service.OrderService;

/**
 * @author jinwei.lin
 * 
 */
public class CrmQueryAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 178887580319281383L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(CrmQueryAction.class);

	@Autowired
	private LogService logService;

	private OrderService orderService;
	private DomainService<Menu> menuService; // 與 setter

	private String outerOrderId;

	/**
	 * @return the outerOrderId
	 */
	public String getOuterOrderId() {
		return outerOrderId;
	}

	/**
	 * @param outerOrderId the outerOrderId to set
	 */
	public void setOuterOrderId(String outerOrderId) {
		this.outerOrderId = outerOrderId;
	}

	public Menu getMemberStatus() {
		return memberStatus;
	}

	public void setMemberStatus(Menu memberStatus) {
		this.memberStatus = memberStatus;
	}

	public Menu getMemberRank() {
		return memberRank;
	}

	public void setMemberRank(Menu memberRank) {
		this.memberRank = memberRank;
	}

	public Menu getOrderStMenu() {
		return orderStMenu;
	}

	public void setOrderStMenu(Menu orderStMenu) {
		this.orderStMenu = orderStMenu;
	}

	public Menu getPaymentStMenu() {
		return paymentStMenu;
	}

	public void setPaymentStMenu(Menu paymentStMenu) {
		this.paymentStMenu = paymentStMenu;
	}

	public Menu getPaymentTpMenu() {
		return paymentTpMenu;
	}

	public void setPaymentTpMenu(Menu paymentTpMenu) {
		this.paymentTpMenu = paymentTpMenu;
	}

	public Menu getLogisticStMenu() {
		return logisticStMenu;
	}

	public void setLogisticStMenu(Menu logisticStMenu) {
		this.logisticStMenu = logisticStMenu;
	}

	public Menu getReturnStMenu() {
		return returnStMenu;
	}

	public void setReturnStMenu(Menu returnStMenu) {
		this.returnStMenu = returnStMenu;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	private MemberService memberService;
	private List<Orders> orders;
	private List<Member> members;
	private SimplePager pager;

	// 查詢條件
	private String orderIdParam;
	private String loginIdParam;
	private String emailParam;
	private String mobileParam;
	private String receiverName;
	private String resultMsg;
	/*************** init menu ******************/
	private Menu memberStatus;
	private Menu memberRank;
	private Menu orderStMenu; // 與 getter, setter
	private Menu paymentStMenu; // 與 getter, setter
	private Menu paymentTpMenu; // 與 getter, setter
	private Menu logisticStMenu; // 與 getter, setter
	private Menu returnStMenu; // 與 getter, setter
	private Menu domainSrc;
	private Menu memberSrc;
	private String domainSrcParam;
	private String memberSrcParam;
	private Map<String, String> memberSrcMap;
	private String recvadddress;
	private String phone;
	private String recvadddressLike;
	private String phoneLike;

	public String getDomainSrcParam() {
		return domainSrcParam;
	}

	public void setDomainSrcParam(String domainSrcParam) {
		this.domainSrcParam = domainSrcParam;
	}

	public Menu getDomainSrc() {
		return domainSrc;
	}

	public void setDomainSrc(Menu domainSrc) {
		this.domainSrc = domainSrc;
	}

	public String getResultMsg() {
		return resultMsg;
	}

	public void setResultMsg(String resultMsg) {
		this.resultMsg = resultMsg;
	}

	public String getLoginIdParam() {
		return loginIdParam;
	}

	public String getOrderIdParam() {
		return orderIdParam;
	}

	public List<Orders> getOrders() {
		return orders;
	}

	public SimplePager getPager() {
		return pager;
	}

	public Menu getMemberSrc() {
		return memberSrc;
	}

	public void setMemberSrc(Menu memberSrc) {
		this.memberSrc = memberSrc;
	}

	public String getMemberSrcParam() {
		return memberSrcParam;
	}

	public void setMemberSrcParam(String memberSrcParam) {
		this.memberSrcParam = memberSrcParam;
	}

	public Map<String, String> getMemberSrcMap() {
		return memberSrcMap;
	}

	public void setMemberSrcMap(Map<String, String> memberSrcMap) {
		this.memberSrcMap = memberSrcMap;
	}

	public void prepare() throws Exception {
		if (null == memberStatus) {
			memberStatus = menuService.get("Member.Status");
		}

		if (null == memberRank) {
			memberRank = menuService.get("Member.Rank");
		}

		if (orderStMenu == null) {
			orderStMenu = menuService.get("Order.status.md");
		}
		if (logisticStMenu == null) {
			logisticStMenu = menuService.get("Order.Prod.LogisticSt");
		}
		if (paymentStMenu == null) {
			paymentStMenu = menuService.get("Order.paymentSt");
		}
		if (paymentTpMenu == null) {
			paymentTpMenu = menuService.get("Order.PaymentTp");
		}
		if (returnStMenu == null) {
			returnStMenu = menuService.get("Order.Prod.ReturnSt");
		}
		if (null == domainSrc) {
			domainSrc = menuService.get("Site.domain");
		}
		if (null == memberSrc) {
			memberSrc = menuService.get("Member.Source");
		}
		clearErrorsAndMessages();
	}

	/**
	 * 订单資料列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryOrder() throws Exception {
		clearErrorsAndMessages();
		if (orders != null) {
			orders.clear();
		}
		CommonCriteria critOrder = new CommonCriteria();
		CommonCriteria critMember = new CommonCriteria();
		resultMsg = "";
		// if (StringUtils.isBlank(orderIdParam) && StringUtils.isBlank(loginIdParam) && StringUtils.isBlank(emailParam)) {
		// resultMsg = "您未输入订单编号会员帐号或会员email";
		// return "list";
		// }
		if (StringUtils.isNotBlank(loginIdParam)) {
			critMember.addEq("loginId", loginIdParam);
		}
		if (StringUtils.isNotBlank(emailParam)) {
			critMember.addEq("email", emailParam);
		}
		if (StringUtils.isNotBlank(mobileParam)) {
			critMember.addEq("mobile", mobileParam);
		}
		if (StringUtils.isNotBlank(receiverName)) {
			critMember.addEq("name", receiverName);
		}
		if (StringUtils.isNotBlank(memberSrcParam)) {
			if ("yaodian100".equals(memberSrcParam)) {
				if (memberSrc != null) {
					Map<String, Option> ops = memberSrc.getOptions();
					for (Option op : ops.values()) {
						if (!"yaodian100".equals(op.getCode())) {
							critMember.addNe("applySource", op.getCode());
						}
					}
				}

			} else {
				critMember.addEq("applySource", memberSrcParam);
			}

		}
		logService.save(getClass().getSimpleName(), "queryOrder", critMember);
		if (StringUtils.isNotBlank(loginIdParam) || StringUtils.isNotBlank(emailParam)) {
			members = memberService.getList(0, -1, critMember, null);
			if (members.size() == 0) {
				resultMsg = "查无此会员";
				return "list";
			} else {
				memberSrcMap = new HashMap<String, String>();
				for (Member member : members) {
					memberSrcMap.put(member.getMemberId(), member.getApplySource());
				}
				critOrder.addEq("memberId", members.get(0).getMemberId());
			}
			// } else {
			// critOrder.addEq("memberId", members.get(0).getMemberId());
			// }
		}
		if (StringUtils.isNotBlank(orderIdParam)) {
			critOrder.addEq("orderId", orderIdParam);
		}
		if (StringUtils.isNotBlank(mobileParam)) {
			critOrder.addEq("receiverMobile", mobileParam);
		}
		if (StringUtils.isNotBlank(loginIdParam)) {
			critOrder.addEq("loginId", loginIdParam);
		}
		if (StringUtils.isNotBlank(emailParam)) {
			critOrder.addEq("receiverEmail", emailParam);
		}
		if (StringUtils.isNotBlank(receiverName)) {
			critOrder.addEq("receiverName", receiverName);
		}
		if (StringUtils.isNotBlank(domainSrcParam)) {
			critOrder.addEq("domainSrc", domainSrcParam);
		}
		if (StringUtils.isNotBlank(phone)) {
			if ("Y".equals(phoneLike)) {
				critOrder.addRlike("receiverPhone", phone + "%");
			} else {
				critOrder.addEq("receiverPhone", phone);
			}
		}
		if (StringUtils.isNotBlank(recvadddress)) {

			if ("Y".equals(recvadddressLike)) {
				critOrder.addRlike("receiverAddr", recvadddress + "%");
			} else {
				critOrder.addEq("receiverAddr", recvadddress);
			}
		}

		if (StringUtils.isNotBlank(outerOrderId)) {
			outerOrderId = outerOrderId.trim();
			critOrder.addEq("outerOrderId", outerOrderId);
		}
		if (pager == null) {
			pager = new SimplePager();
		}
		// pager.setPageRecord(1);
		int entitySize = orderService.getListSize(critOrder).intValue();
		pager.setTotalSize(entitySize);
		orders = orderService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(), critOrder,
				null);
		// logger.info("pager:{},orders:{}", pager, orders);
		if (members != null) {
			members.clear();
		}

		return "list";
	}

	/**
	 * 订单資料列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String queryMember() throws Exception {
		if (members != null) {
			members.clear();
		}
		CommonCriteria critMember = new CommonCriteria();
		resultMsg = "";

		if (StringUtils.isNotBlank(loginIdParam)) {
			critMember.addEq("loginId", loginIdParam);
		}
		if (StringUtils.isNotBlank(emailParam)) {
			critMember.addEq("email", emailParam);
		}
		if (StringUtils.isNotBlank(mobileParam)) {
			critMember.addEq("mobile", mobileParam);
		}
		if (StringUtils.isNotBlank(receiverName)) {
			critMember.addEq("name", receiverName);
		}
		if (StringUtils.isNotBlank(domainSrcParam)) {
			critMember.addEq("domainSrc", domainSrcParam);
		}
		if (StringUtils.isNotBlank(memberSrcParam)) {
			if ("yaodian100".equals(memberSrcParam)) {
				if (memberSrc != null) {
					Map<String, Option> ops = memberSrc.getOptions();
					for (Option op : ops.values()) {

						if (!"yaodian100".equals(op.getCode())) {
							critMember.addNe("applySource", op.getCode());
						}
					}
				}

			} else {
				critMember.addEq("applySource", memberSrcParam);
			}

		}

		logService.save(getClass().getSimpleName(), "queryMember", critMember);
		members = memberService.getList(0, -1, critMember, null);
		if (members.size() == 0) {
			resultMsg = "查无此会员";
			return "list";
		}
		if (pager == null) {
			pager = new SimplePager();
		}
		int entitySize = memberService.getListSize(critMember).intValue();
		pager.setTotalSize(entitySize);
		// logger.info("pager:{},orders:{}", pager, orders);
		if (orders != null) {
			orders.clear();
		}
		return "list";
	}

	/**
	 * 查询
	 * 
	 * @return
	 * @throws Exception
	 */
	public String search() throws Exception {
		// setDomainSrcParam(ServerValue.getFrontHostname().substring(7));
		return "search";
	}

	public void setLoginIdParam(String loginIdParam) {
		this.loginIdParam = loginIdParam;
	}

	public void setOrderIdParam(String orderIdParam) {
		this.orderIdParam = orderIdParam;
	}

	public void setOrders(List<Orders> orders) {
		this.orders = orders;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public List<Member> getMembers() {
		return members;
	}

	public void setMembers(List<Member> member) {
		this.members = members;
	}

	public String getEmailParam() {
		return emailParam;
	}

	public void setEmailParam(String emailParam) {
		this.emailParam = emailParam;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public String getMobileParam() {
		return mobileParam;
	}

	public void setMobileParam(String mobileParam) {
		this.mobileParam = mobileParam;
	}

	/**
	 * @return the receiverName
	 */
	public String getReceiverName() {
		return receiverName;
	}

	/**
	 * @param receiverName the receiverName to set
	 */
	public void setReceiverName(String receiverName) {
		this.receiverName = receiverName;
	}

	/**
	 * @return the recvadddress
	 */
	public String getRecvadddress() {
		return recvadddress;
	}

	/**
	 * @param recvadddress the recvadddress to set
	 */
	public void setRecvadddress(String recvadddress) {
		this.recvadddress = recvadddress;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the recvadddressLike
	 */
	public String getRecvadddressLike() {
		return recvadddressLike;
	}

	/**
	 * @param recvadddressLike the recvadddressLike to set
	 */
	public void setRecvadddressLike(String recvadddressLike) {
		this.recvadddressLike = recvadddressLike;
	}

	/**
	 * @return the phoneLike
	 */
	public String getPhoneLike() {
		return phoneLike;
	}

	/**
	 * @param phoneLike the phoneLike to set
	 */
	public void setPhoneLike(String phoneLike) {
		this.phoneLike = phoneLike;
	}

}